package Visao;

import Modelo.Hospede;
import RMI.ServicosCliente;
import RMI.ServicosIF;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo Veinberg <pveinberg@gmail.com>
 */
public class FormCobranca extends javax.swing.JDialog {

    private Hospede hospede; 

    public FormCobranca(Hospede h) {
        initComponents();
        setLocationRelativeTo(null);
        this.hospede = h;
        titulo.setText("Ingressar pagamento de " + h.getNome() + "(R$ " + h.getSaldo() + ")");
    }

    public Hospede getHospede() {
        return hospede;
    }

    public void setHospede(Hospede hospede) {
        this.hospede = hospede;
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        confirma = new javax.swing.JButton();
        cancela = new javax.swing.JButton();
        titulo = new javax.swing.JLabel();
        valor = new javax.swing.JTextField();

        setTitle("Cadastro de Quartos");
        setModal(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        confirma.setText("Confirma");
        confirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmaActionPerformed(evt);
            }
        });

        cancela.setText("Cancela");
        cancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaActionPerformed(evt);
            }
        });

        titulo.setText("sem_data");

        valor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(224, 224, 224)
                        .addComponent(confirma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancela))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(titulo))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(titulo)
                .addGap(18, 18, 18)
                .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirma)
                    .addComponent(cancela))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void confirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmaActionPerformed

        // seta o quarto como ocupado
        
        ServicosIF sc = ServicosCliente.getInstance();
        
        try {           
        
            double d = Double.parseDouble(valor.getText()); 
            this.hospede.setSaldo(hospede.getSaldo()- d);
            valor.setText("");
            
            if (sc.editHospede(hospede)) {
                JOptionPane.showMessageDialog(null, "Cobrança realizada com sucesso");
            } else {
                JOptionPane.showMessageDialog(null, "Não foi posível computar a cobrança.");
            }
            
        } catch (RemoteException ex) {
            Logger.getLogger(FormCobranca.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        this.setVisible(false);        
        this.dispose();
    }//GEN-LAST:event_confirmaActionPerformed

    private void cancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_cancelaActionPerformed

    private void valorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valorActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancela;
    private javax.swing.JButton confirma;
    private javax.swing.JLabel titulo;
    private javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
}
